import { 
	ApiGetUnBindStoreList,
	ApiGetStoreDetail,
	ApiGetStoreClassList,
	ApiGetStoreAdvList,
	ApiGetAppDictList,
	ApiGetBindStoreList
 } from '@/common/request/api/shop.js'
import { 
	ApiGetGoodsSearchList
 } from '@/common/request/api/goods.js'
 import { goodsContorl } from '@/common/utils/goods.js'
import {
	STORE_UNBINDLIST,
	STORE_BINDLIST,
	STORE_DETAIL,
	STORE_CLASSLIST,
	STORE_ADVLIST,
	STORE_GOODSLIST,
	LOADING_STATUS,
	SHOPPINGCODELIST,
	PAYTYPELIST,
	ORDERSTATUSLIST,
	DRUGRELATIONLIST,
	STORE_GOODSLOADING_STATUS,
	STOREGOODS_UPDATECARTNUM,
	STOREGOODS_CLEARCARTNUM,
	LOGIN_ADVLIST
} from "../types.js"
import {
	IMG_BASEURL
} from "../../utils/config.js"
const state = {
	storeUnbindList:{
		records:[],
		data:[],
		current:1,
		pages:0,
		size:20,
		total:0
	},
	loadingStatus:'loading',
	storeDetail:{},
	storeClassList:[],
	storeAdvList:[],
	storeBindList:[],
	shoppingCodeList:[],
	payTypeList:[],
	orderStatusList:[],
	drugRelationList:[],
	storeGoodsList:{
		records:[],
		data:[],
		current:1,
		pages:0,
		size:20,
		total:0
	},
	storeGoodsLoadingStatus:'loading',
	loginAdvList:[]
}
const actions = {
	// δ�󶨵����б�
	async ActGetStoreUnBindList({commit},payload){
		commit('LOADING_STATUS','loading')
		const res = await ApiGetUnBindStoreList(payload)
		if(res.success){
			commit('STORE_UNBINDLIST',res.data)
		}
	},
	// ������Ϣ
	async ActGetStoreDetail({commit},payload){
		const res = await ApiGetStoreDetail(payload)
		if(res.success){
			commit('STORE_DETAIL',res.data)
		}
	},
	// ���̷���
	async ActGetStoreClassList({commit},payload){
		const res = await ApiGetStoreClassList(payload)
		if(res.success){
			commit('STORE_CLASSLIST',res.data)
		}
	},
	// ���̹��
	async ActGetStoreAdvList({commit},payload){
		const res = await ApiGetStoreAdvList(payload)
		if(res.success){
			let newData = []
			res.data.map((item,index)=>{
				console.log(IMG_BASEURL,item.resUrl)
				newData.push({...item,image:IMG_BASEURL+item.resUrl})
			})
			if (payload.apKey==='Top') {
				commit('STORE_ADVLIST',newData)
			} else if (payload.apKey==='advLogin') {
				commit('LOGIN_ADVLIST',newData)
			}
			
		}
	},
	// �Ѱ󶨵����б�
	async ActGetStoreBindList({commit},payload){
		const res = await ApiGetBindStoreList(payload)
		if(res.success){
			commit('STORE_BINDLIST',res.data)
		}
	},
	// ��ȡ�ֵ�
	async ActGetAppDictList({commit},payload){
		const res = await ApiGetAppDictList(payload)
		console.log(payload)
		if(res.success){
			if(payload.dictType == 'shipping_code'){
				console.log(res.data)
				commit('SHOPPINGCODELIST',res.data)
			}else if(payload.dictType == 'pay_type'){
				commit('PAYTYPELIST',res.data)
			}else if(payload.dictType == 'orderStatus'){
				commit('ORDERSTATUSLIST',res.data)
			}else if(payload.dictType == 'drug_relation'){
				commit('DRUGRELATIONLIST',res.data)
			}
		}else{
			uni.showModal({
				title: '��ʾ',
				content: res.msg,
				showCancel:false
			});
		}
	},
	// ������Ʒ�б�
	async ActGetStoreGoodsList({commit},payload){
		commit('STORE_GOODSLOADING_STATUS','loading')
		if (payload.current===1) {
			commit('STORE_GOODSLIST','reset')	
		}
		const res = await ApiGetGoodsSearchList(payload)
		if(res.success){
			commit('STORE_GOODSLIST',res.data)
		}
	},
}
const mutations ={
	[STORE_UNBINDLIST](state,data){
		if(data.current === 1){
			data.data = data.records
		}else{
			data.data = state.storeUnbindList.data.concat(data.records)
		}
		if(data.current*data.size<data.total){
			state.loadingStatus = 'loadmore'
		}else{
			state.loadingStatus = "nomore"
		}
		state.storeUnbindList = data
	},
	[STORE_DETAIL](state,data){
		state.storeDetail = data
	},
	[STORE_CLASSLIST](state,data){
		let arr = [{gcId:'全部',gcName:'全部'}]
		state.storeClassList = arr.concat(data)
		if (data.length===0) {
			state.storeGoodsLoadingStatus = "nomore"
		}
	},
	[STORE_ADVLIST](state,data){
		state.storeAdvList = data
	},
	[LOGIN_ADVLIST](state,data){
		state.loginAdvList = data
	},
	[STORE_BINDLIST](state,data){
		state.storeBindList = data
	},
	[LOADING_STATUS](state, data){
		state.loadingStatus = data
	},
	[SHOPPINGCODELIST](state, data){
		state.shoppingCodeList = data
	},
	[PAYTYPELIST](state, data){
		state.payTypeList = data
	},
	[ORDERSTATUSLIST](state, data){
		state.orderStatusList = data
	},
	[DRUGRELATIONLIST](state, data){
		state.drugRelationList = data
	},
	[STORE_GOODSLIST](state,data){
		if (data==='reset') {
			state.storeGoodsList = {
				records:[],
				data:[],
				current:1,
				pages:0,
				size:20,
				total:0
			}
		}else {
			if(data.current === 1){
				data.data = data.records
			}else{
				data.data = state.storeGoodsList.data.concat(data.records)
			}
			if(data.current*data.size<data.total){
				state.storeGoodsLoadingStatus = 'loadmore'
			}else{
				state.storeGoodsLoadingStatus = "nomore"
			}
			data.data.map((item,index)=>{
				item = goodsContorl(item)
			})
			console.log('datadata',data)
			state.storeGoodsList = data
		}
		
	},
	[STORE_GOODSLOADING_STATUS](state, data){
		state.storeGoodsLoadingStatus = data
	},
	[STOREGOODS_UPDATECARTNUM](state, data){
		data.map((dataItem,dataIndex)=>{
			// �޸���Ʒ�б��еĹ��ﳵ����
			state.storeGoodsList.data.map((goodsItem, goodsIndex)=>{
				if(goodsItem.goodsId == dataItem.goodsId){
					goodsItem.goodsNum=dataItem.num
					return
				}
			})
		})
	},
	[STOREGOODS_CLEARCARTNUM](state, data){
		state.storeGoodsList.data.map((goodsItem, goodsIndex)=>{
				goodsItem.goodsNum=0
		})
	},
}
const getters = {}

export default {
	state,
	mutations,
	actions,
	getters
}